import {defineStore} from "pinia";
import enumeration from "@/store/enum";


export  const useDataStore=defineStore('data',{
    state:()=>{
        return {
            insuranceData:{},
            queryCondition:null,
            username:{},
            gotoUrl:null,
            chooseType:null,
            controlBtn:null,
            menu:{},
            tabMenu:<any>[],
            sensorsOpen:false,
            assessorCompany:[],
            enumeration:<any>[],
            areaLists:<any>[],
            allProduct:[],
            currencyRate:[]
        }
    },
    getters:{
        getUserCode(state){
            if(state.username){
                return state.username
            }else{
                if(localStorage.getItem('username')){
                    return JSON.parse(localStorage.getItem('username')||'{}')
                }
            }
        },
        getQueryCondition(state){
            if(state.queryCondition){
                return state.queryCondition
            }else{
                if(localStorage.getItem("queryCondition")){
                    return JSON.parse(localStorage.getItem("queryCondition")||'{}')
                }
            }
        },
        getUrl (state) {
            if(state.gotoUrl){
                return state.gotoUrl
            }else{
                if(localStorage.getItem("gotoUrl")){
                    return JSON.parse(localStorage.getItem("gotoUrl")||'{}')
                }
            }
        },
        getChooseType (state) {
            if(state.chooseType){
                return state.chooseType
            }else{
                if(localStorage.getItem("chooseType")){
                    return JSON.parse(localStorage.getItem("chooseType")||'{}')
                }
            }
        },
        getControlBtn (state) {
            if(state.controlBtn){
                return state.controlBtn
            }else{
                if(localStorage.getItem("controlBtn")){
                    return JSON.parse(localStorage.getItem("controlBtn")||'{}')
                }
            }
        },
        getMenu(state){
            if(state.menu){
                return state.menu
            }else{
                if(localStorage.getItem("menu")){
                    return JSON.parse(localStorage.getItem("menu")||'{}')
                }
            }
        },
        getTabMenu (state) {
            if(state.tabMenu){
                return state.tabMenu
            }else{
                if(localStorage.getItem("tabMenu")){
                    return JSON.parse(localStorage.getItem("tabMenu")||'{}')
                }
            }
        },
        getSensorsOpen (state) {
            if(state.sensorsOpen){
                return state.sensorsOpen
            }else{
                if(localStorage.getItem("sensorsOpen")){
                    return JSON.parse(localStorage.getItem("sensorsOpen")||'{}')
                }
            }
        },
        getAssessorCompany (state) {
            if(state.assessorCompany){
                return state.assessorCompany
            }else{
                if(localStorage.getItem("assessorCompany")){
                    return JSON.parse(localStorage.getItem("assessorCompany")||'{}')
                }
            }
        },
        getEnumeration (state) {
            if(state.enumeration){
                return state.enumeration
            }else{
                if(localStorage.getItem("enumeration")){
                    return JSON.parse(localStorage.getItem("enumeration")||'{}')
                }
            }
        },
        getAreaLists (state) {
            if(state.areaLists){
                return state.areaLists
            }else{
                if(localStorage.getItem("areaLists")){
                    return JSON.parse(localStorage.getItem("areaLists")||'{}')
                }
            }
        },
        getAllProduct (state) {
            if(state.allProduct){
                return state.allProduct
            }else{
                if(localStorage.getItem("allProduct")){
                    return JSON.parse(localStorage.getItem("allProduct")||'{}')
                }
            }
        },
        getCurrencyRate (state) {
            if(state.currencyRate){
                return state.currencyRate
            }else{
                if(localStorage.getItem("currencyRate")){
                    return JSON.parse(localStorage.getItem("currencyRate")||'{}')
                }
            }
        }
    },
    actions:{
        saveControlBtn(controlBtn){
            // @ts-ignore
            this.controlBtn=false
            localStorage.setItem("controlBtn",JSON.stringify(controlBtn))
        },
        saveQueryCondition(queryCondition){
            this.queryCondition=queryCondition
            localStorage.setItem("queryCondition",JSON.stringify(queryCondition))
        },
        saveUrl(gotoUrl){
            this.gotoUrl=gotoUrl
            localStorage.setItem("gotoUrl",JSON.stringify(gotoUrl))
        }
    },
    persist:{
        enabled:true,
        strategies:[
            {
                key:'saveUserCode',
                storage:localStorage,
                paths:['username']
                // paths:['username','queryCondition','gotoUrl','chooseType','controlBtn','menu','tabMenu','sensorsOpen','assessorCompany','enumeration','areaLists','allProduct','currencyRate']
            },
            {
                key:'saveQueryCondition',
                storage:localStorage,
                paths:['queryCondition']
            },
            {
                key:'saveUrl',
                storage:localStorage,
                paths:['gotoUrl']
            },
            {
                key:'saveChooseType',
                storage:localStorage,
                paths:['chooseType']
            },
            {
                key:'saveControlBtn',
                storage:localStorage,
                paths:['controlBtn']
            },
            {
                key:'saveMenu',
                storage:localStorage,
                paths:['menu']
            },
            {
                key:'saveTabMenu',
                storage:localStorage,
                paths:['tabMenu']
            },
            {
                key:'saveSensorsOpen',
                storage:localStorage,
                paths:['sensorsOpen']
            },
            {
                key:'saveAssessorCompany',
                storage:localStorage,
                paths:['assessorCompany']
            },
            {
                key:'saveEnumeration',
                storage:localStorage,
                paths:['enumeration']
            },
            {
                key:'saveAreaLists',
                storage:localStorage,
                paths:['areaLists']
            },
            {
                key:'saveAllProduct',
                storage:localStorage,
                paths:['allProduct']
            },
            {
                key:'saveCurrencyRate',
                storage:localStorage,
                paths:['currencyRate']
            }
        ],
    }
})
